/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.TestPcModel;
import org.testis.gui.mvc.view.testbed.TestPcView;
import org.testis.orm.testbed.refined.TestPcRefined;
import org.testis.orm.testbed.datahandlers.TestPcDataHandler;
import org.testis.orm.testbed.refined.TestPcFactory;
import org.testis.utils.tree.TbedMutableTreeNode;

/**
 *
 * @author selcuk
 */
public class TestPcController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String TESTPC_ID_PROPERTY = "TestPcId";
    public static final String NAME_PROPERTY = "Name";
    private TbedMutableTreeNode m_selectedNode = null;

    public TestPcController() {
        super();
    }

    private void initialize(boolean editMode) {
        TestPcModel model = new TestPcModel();
        TestPcView view = new TestPcView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationEditTestPc(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof TestPcRefined) {
            m_selectedNode = node;
            TestPcRefined testPc = getSelectedTestPc();
            if (testPc == null) {
                System.out.println(
                        "Not found design part for TestPcRefined");
                return;
            }
            initialize(true);
            setModelProperty(TESTPC_ID_PROPERTY, testPc.getId());
            setModelProperty(NAME_PROPERTY, testPc.getName());
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationNewTestPc() {
        initialize(false);
        setModelProperty(
                VISIBLE_PROPERTY, true);
    }

    public void operationSaveTestPc(int id, String name) {
        boolean result = TestPcDataHandler.newInstance().saveTestPc(id, name);
        if (!result) {
            setModelProperty(ERROR_PROPERTY, "Unable to edit TestPc record");
        }
    }

    public void operationAddTestPc(String name) {
        TestPcRefined refined = TestPcFactory.newInstance().createTestPc(name);
        if (refined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    refined);
        } else {
            setModelProperty(ERROR_PROPERTY, "Unable to add TestPc record");
        }
    }

    public TestPcRefined getSelectedTestPc() {
        TestPcRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof TestPcRefined) {
            result = (TestPcRefined) obj;
        }
        return result;
    }

    public void operationRemoveTestPc(int id) {
        if (TestPcDataHandler.newInstance().deleteTestPc(id)) {
            TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove device record");
        }
    }
}
